/* global appConfig */
require(["common", "hash_routie", "coAjax", "queryString", "jQuery", "browser", "href", "eventManager", "WX", "/js/common/goodsCart.js"], function(jSouper, hash_routie, coAjax, QueryString, $, browser, href, eventManager, WX, goodsCart) {
	/*
	 * HASH路由
	 */
	// personal路由
	hash_routie({
		html_url: "./pages/mb/personal/",
		js_url: "./js/mobile/personal/",
		hash_prefix: "personal/:page",
		use_css: "./css/mobile.personal.css",
		// default_hash: "order",
		teleporter: "main"
	});
	// main路由
	hash_routie({
		html_url: "./pages/mb/default/",
		js_url: "./js/mobile/default/",
		hash_prefix: "default/:page",
		use_css: "./css/mobile.main.css",
		default_hash: "main",
		teleporter: "main"
	});
	// 缩放控制
	hash_routie.on("*", function() {
		document.body.style.zoom = 1;
		console.log("hash_routie_key", hash_routie._current_key)
		switch (hash_routie._current_key) {
			case "goods_details":
				document.getElementById("vp").content = "width=device-width, initial-scale=1.0, user-scalable=yes";
				break;
			default:
				document.getElementById("vp").content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no";
				break;
		};
	});
	//微信分享的配置
	WX(function(wx) {
		hash_routie.on("*", function(page) {
			// wx.error(function(res) {
			// 	alert('wx.error: ' + JSON.stringify(res));
			// });
			switch (hash_routie._current_key) {
				case "goods_details":
					var share_config = {
						title: App.get("goods_detail.goods_name") + "【" + App.get("bus_info.info.title") + "】",
						desc: App.get('goods_detail.intro'),
						link: location.host.toString() + "/mobile.main.html#default/goods_details?id=" + App.get('goods_detail._id') + "&rcid=" + App.get("loginer._id"),
						imgUrl: App.get("goods_detail.preview_img_url") + "?imageView/1/w/300/h/300",
					};
					break;
				default:
					share_config = {
						// title: App.get("bus_info.info.title") + "【点纳微站】",
						title: App.get("bus_info.info.title"),
						desc: App.get("bus_info.info.intro"),
						link: location.origin + "/mobile.main.html#" + (location.hash.split("#")[1] || "default/main") + "?rcid=" + (App.get("loginer._id") || ""),
						imgUrl: App.get("bus_info.info.logo_url") + "?imageView/1/w/300/h/300"
					};
			};
			// debugger
			// alert(share_config.title);
			share_config.trigger = function(res) {
				alert('用户点击发送给朋友');
			};
			share_config.success = function(res) {
				alert("success", '已分享');
			};
			share_config.cancel = function(res) {
				alert('分享已取消');
			};
			share_config.fail = function(res) {
				alert(JSON.stringify(res));
			};

			// 分享给好友
			wx.onMenuShareAppMessage(share_config);
			// 分享到朋友圈
			wx.onMenuShareTimeline(share_config);
			// 分享到QQ
			wx.onMenuShareQQ(share_config);
		});
		// // 按钮隐藏
		// wx.hideMenuItems({
		// 	menuList: [
		// 		'menuItem:readMode', // 阅读模式
		// 		'menuItem:copyUrl', // 复制链接
		// 		'menuItem:share:email' // 通过邮件分享
		// 	],
		// 	success: function(res) {
		// 		// alert('已隐藏“阅读模式”，“复制链接”等按钮');
		// 	},
		// 	fail: function(res) {
		// 		alert(JSON.stringify(res));
		// 	}
		// });
	});



	//获取指定页的商品
	coAjax.get(appConfig.goods.list, {
		bus_id: appConfig.bus_id,
		num: 6,
		page: 0
	}, function(result) {
		var goods_list = result.result;
		App.set("goods_list", goods_list);
	}, function() {
		console.log(arguments);
	});

	// 获取分类树
	coAjax.get(appConfig.bus_classify_tree, {
		bus_id: appConfig.bus_id
	}, function(tree) {
		console.log(tree)
		var classify_origin_list = tree.result.childs;
		App.set("$Cache.main.classify.tree", classify_origin_list);
		classify_origin_list.forEach(function(item, index) {
			coAjax.get(appConfig.bus_classify_all_child_goods_by_id + item.classify._id, {
				bus_id: appConfig.bus_id
			}, function(classify_item) {
				App.concat("$Cache.main.classify_with_goods", [{
					_id: item.classify._id,
					title: item.classify.classify_name,
					contents: classify_item.result
				}]);
			});
		});
	});
	App.set('$Event.href', function() {
		if (App.get('$Cache.search_text')) {
			href.jump("#default/searchset?q=" + App.get('$Cache.search_text'))
		} else {
			alert('error', '请输入搜索关键词');
		}
	})

	hash_routie.on("main", function() {
		App.set("thisPage", 0);
	});

	function _get_cart_success(result) {
		var cart_detail = result.result;
		App.set("user.cart_detail", cart_detail);
		href.jump("/mobile.main.html#default/cart");
	};

	App.set("$Event.goods_list.add_to_cart", function(e, vm) {
		var cart_item = {
			goods_id: vm.get("_id"),
			num: 1
		};
		goodsCart.add_to_cart(vm.get("_id"), 1, _get_cart_success)
	});

	// 获取标签详情
	// coAjax.get(appConfig.bus.tags_detail, {
	// 	bus_id: appConfig.bus_id
	// }, function(result) {
	// 	App.set("$Cache.main.tags", result.result);
	// });

	// 抽奖内容
	coAjax.get(appConfig.bus_lottery_list, {
		bus_id: appConfig.bus_id,
		with_award_content_detail: true
	}, function(result) {
		App.set("$Cache.main.drawlist", result.result)
	});
});